//  Android Carpool System
//  2011.5-2011.5
//  Author Jinhuan Wang
//  name: ViewMap.java
//  description: draw  route on a map 
//$history: /carpool/scr/com/amelie/driving/impl/MyOverlay.java$
package com.carpool.driving.impl;

import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Point;

import com.carpool.R;
import com.carpool.driving.DrivingDirections;
import com.carpool.driving.Placemark;
import com.carpool.driving.Route;
import com.carpool.driving.DrivingDirections.Mode;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class MyOverlay extends Overlay {
	private Route route = null;
	private DrivingDirections.Mode mode = null;

	public void setRoute(Route route, DrivingDirections.Mode mode) {
		this.route = route;
		this.mode = mode;
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);

		if (route != null) {
			drawRoute(route.getGeoPoints(), canvas, mapView);

			drawPlacemarks(route.getPlacemarks(), canvas, mapView);
		}

	}

	private void drawRoute(List<GeoPoint> geoPoints, Canvas canvas,
			MapView mapView) {
		for (int i = 0; i < geoPoints.size() - 1; i++) {
			// Convert the starting geographical point coordinates to screen
			// coordinates.
			//
			GeoPoint startGeoPoint = geoPoints.get(i);
			Point startScreenPoint = new Point();
			mapView.getProjection().toPixels(startGeoPoint, startScreenPoint);

			// Convert the ending geographical point coordinates to screen
			// coordinates.
			//
			GeoPoint endGeoPoint = geoPoints.get(i + 1);
			Point endScreenPoint = new Point();
			mapView.getProjection().toPixels(endGeoPoint, endScreenPoint);

			// Draw a joining line between the starting and the ending points.
			//
			Paint paint = new Paint();
			paint.setStrokeWidth(5);
			paint.setAntiAlias(true);
			if ((mode != null) && (mode == Mode.DRIVING)) {
				paint.setARGB(64, 0, 0, 255);
			} else {
				paint.setARGB(96, 0, 0, 0);
				paint.setPathEffect(new DashPathEffect(new float[] { 8, 4 }, 1));
			}
			canvas.drawLine(startScreenPoint.x, startScreenPoint.y,
					endScreenPoint.x, endScreenPoint.y, paint);
		}
	}

	private void drawPlacemarks(List<Placemark> placemarks, Canvas canvas,
			MapView mapView) {
		for (int i = 0; i < placemarks.size(); i++) {
			// Convert the placemark geographical location to screen
			// coordinates.
			//
			GeoPoint geoPoint = (placemarks.get(i)).getLocation();
			Point screenPoint = new Point();
			mapView.getProjection().toPixels(geoPoint, screenPoint);

			// Select the placemark to use (start, middle or end points) and
			// draw its bitmap
			// on the screen.
			//
			Bitmap place;

			if (i == 0) {
				// Start placemark.

				place = BitmapFactory.decodeResource(mapView.getResources(),
						R.drawable.startico);
				canvas.drawBitmap(place, screenPoint.x, screenPoint.y, null);
			} else if (i == placemarks.size() - 1) {
				// End placemark.

				place = BitmapFactory.decodeResource(mapView.getResources(),
						R.drawable.endico);
				canvas.drawBitmap(place, screenPoint.x, screenPoint.y, null);
			} else {
				// Middle placemark.

				place = BitmapFactory.decodeResource(mapView.getResources(),
						R.drawable.middleicon);
				canvas.drawBitmap(place, screenPoint.x, screenPoint.y, null);
			}
		}
	}
}
